package com.refusea.train.domain.helper

import com.refusea.train.domain.cache.TokenCache
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component

/**
 * @author Zhang Yan 2024/3/26
 **/
@Component
class LogoutHelper(private val tokenCache: TokenCache) {

    private val log: Logger = LoggerFactory.getLogger(LogoutHelper::class.java)

    fun logout(authentication: String?) {
        if (authentication == null) {
            log.warn("logout: authentication is null")
            return
        }

        val token = authentication.substringAfter("Bearer ")
        try {
            tokenCache.remove(token)
        } catch (e: Exception) {
            log.error("Logout error: {}", e.message, e)
        }

        log.debug("Logout successful, token={}", token)
    }

}
